package com.jason.shader.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.jason.shader.R;

/**
 * @DESC:
 * @Author: Jason
 * @Date: 16/9/3
 * @Time: 16:06
 */

public class BrickView2 extends View {

    private Paint mFillPaint, mStrokePaint; // 填充和描边的画笔
    private BitmapShader mBitmapShader; // bitmap着色器

    private float posX,posY; // 触摸点的xy坐标

    public BrickView2(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 初始化画笔
        initPaint();

    }

    private void initPaint() {
        // 实例化描边画笔并设置参数
        mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mStrokePaint.setColor(0xff000000);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeMiter(5);

        // 实例化填充画笔
        mFillPaint = new Paint();

        // 生成bitmap
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.brick);
        mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        mFillPaint.setShader(mBitmapShader);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 设置画笔背景色
        canvas.drawColor(Color.DKGRAY);

        // 绘制圆和描边
        canvas.drawCircle(posX, posY, 300, mFillPaint);
        canvas.drawCircle(posX, posY, 300, mStrokePaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // 手指移动时获取触摸点坐标并刷新视图
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            posX = event.getX();
            posY = event.getY();

            invalidate();
        }

        return true;
    }
}
